var uid=getcookie("user_uid");
//检验登录权限->
if(uid==null){
alert("cookie异常");
window.location.href="http://localhost:8081/login";}
 $.ajax({
	   url:"simple_user_power_api",
	   method:"post",
	   data:{user_uid:uid
	   },
	   dataType:"json",
	   success:function(data){
	   if(data.status=="fail"){
	   alert("没有登录权限");
	   window.location.href="http://localhost:8081/login";}	   },
	   error:function(data)
	   { alert("登录未知");
	   window.location.href="http://localhost:8081/login";
	   }
	   });
//<-检验登录权限


var switch_obj=getcookie("switch");
var qq;
show_div(switch_obj,true);
$("#icon").html("Hello "+uid);
 $.ajax({
	   url:"manSearch_api",
	   method:"post",
	   data:{user_uid:uid
	   },
	   dataType:"json",
	   success:function(data){
	   var user=data.result;
	   $("#my_name").html("name: "+user.user_name);
	   $("#my_uid").html("uid: "+user.user_uid);
	   $("#my_school_id").html("my_school_id: "+user.user_school_id);
	   $("#my_qq").html("qq: "+user.user_qq);
	   qq=user.user_qq;
	   $("#my_tel").html("tel: "+user.user_tel);
	   $("#my_message").html("签名版: "+user.user_message);
	   $('#from_user_qq').attr('placeholder',user.user_qq);
	   $('#form_user_tel').attr('placeholder',user.user_tel);
	   $('#form_user_message').attr('placeholder',user.user_message);
	   },
	   error:function(data)
	   { alert("no");}
	   });

//cookie大礼包
function setcookie(name, value, time) {
        var exp = new Date();
        exp.setTime(exp.getTime() + time * 1);    
        //设置cookie的名称、值、失效时间
        document.cookie = name + "=" + value + ";expires="+ exp.toGMTString();
         
    }
function getcookie(name) {
        //获取当前所有cookie
       var reg = eval("/(?:^|;\\s*)" + name + "=([^=]+)(?:;|$)/");
       return reg.test(document.cookie) ? RegExp.$1 : "";
    }
function deletecookie(name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        //获取cookie是否存在
        var value = getcookie(name);
        if (value != null) {
            document.cookie = name + "=" + value + ";expires="+ exp.toUTCString();
        }
   }
 //展示持有者的信息  
 function show_this_user(obj){
  var x = $(obj).parent().parent().find("td");
  var item_id = x.eq(0).text()
	$.ajax({
	   url:"search_user_item_api",
	   method:"post",
	   data:{
	   item_id:item_id
	   },
	   dataType:"json",
	   success:function(data){
	   alert(data.status);
	   },
	   error:function(data)
	   { alert("无法查询");}
	   });

 }
 var temp;
 //修改信息
 function change_this(obj){
 $("#change_item_form").show();
  	temp = $(obj).parent().parent().find("td");

 }
 
 function item_submit(){
 var item_id = temp.eq(0).text();
 var from_item_status=$("#from_item_status").val();
var form_item_price=$("#form_item_price").val();
$.ajax({
	   url:"change_item_api",
	   method:"post",
	   data:{
	   item_id:item_id,
	   from_item_status:from_item_status,
	   form_item_price:form_item_price
	   },
	   dataType:"json",
	   success:function(data){
	   alert(data.status);
	   window.location.reload();
	   },
	   error:function(data)
	   { alert("无法修改");}
	   });
 }
 //关闭 
 function item_defalut(){
 $("#change_item_form").hide();
 }
 //删除提交信息
 function delete_this(obj){
   var x = $(obj).parent().parent().find("td");
  	var item_id = x.eq(0).text()
  	$.ajax({
	   url:"delete_item_api",
	   method:"post",
	   data:{
	   item_id:item_id,
	   user_uid:uid
	   },
	   dataType:"json",
	   success:function(data){
	   alert(data.status);
	   window.location.reload();
	   },
	   error:function(data)
	   { alert("无法删除");}
	   });
 }
 function goback(){
	deletecookie("user_uid");
	deletecookie("switch");
 	window.location.href="http://localhost:8081/login";
 }
 function submit(){
   	   var from_user_qq=$("#from_user_qq").val();
	   var form_user_tel=$("#form_user_tel").val();
	   var form_user_message=$("#form_user_message").val();
 		   $.ajax({
	   url:"manChange_api",
	   method:"post",
	   data:{
	   user_uid:uid,
	   user_qq:from_user_qq,
	   user_tel:form_user_tel,
	   user_message:form_user_message
	   },
	   dataType:"json",
	   success:function(data){
	   if(data.status=="success")
	   alert("成功");
	   },
	   error:function(data)
	   { alert("no");}
	   });
 }
 function show(){
 	$("#change_form").show();
 }
 function defalut(){
 	$("#change_form").hide();
 }
 function jump_submit(){
  	   var item_kind=$("#item_kind").val();
	   var item_name=$("#item_name").val();
	   var item_limit_time=$("#item_limit_time").val();
	   var item_price=$("#item_price").val();
 	 if(item_limit_time>30){
 	 alert("输入时间有误");return;}
 	 
 	 $.ajax({
	   url:"release_form_api",
	   method:"post",
	   data:{
	   user_uid:uid,
	   item_kind:item_kind,
	   item_name:item_name,
	   item_limit_time:item_limit_time,
	   item_price:item_price
	   },
	   dataType:"json",
	   success:function(data){
	   if(data.status=="success")
	   alert("成功");
	   else alert(data.status);
	   },
	   error:function(data)
	   { alert(data.status);}
	   });
 }


 //提交修改密码的JS
 function change_pass_submit(){
  	   var change_pass=$("#change_pass_input").val();
	   var change_pass_after=$("#change_pass_after_input").val();
	   var verification=$("#verification").val();
	   if(change_pass_after.length<6){
	   alert("设置密码过短");return;}
	   if(change_pass==change_pass_after){
	   alert("两次密码输入一致");return;}
 	  
 	 $.ajax({
	   url:"change_pass_api",
	   method:"post",
	   data:{
	   user_uid:uid,
	   change_pass:change_pass,
	   change_pass_after:change_pass_after,
	   verification:verification
	   },
	   dataType:"json",
	   success:function(data){
	   if(data.status=="success")
	   alert("成功");
	   else alert(data.status);
	   },
	   error:function(data)
	   { alert("no");}
	   });
 }
 //提交查询后弹出新的窗口显示
  function search_item_submit(){
  	   var item_kind=$("#search_item_kind").val();
	   var item_name=$("#search_item_name").val();
	   var uid_id=$("#search_uid_id").val();
 	
 	 window.open("http://localhost:8081/item?"+"user_uid="+uid_id+"&item_name="+item_name
 	 +"&item_kind="+item_kind,"newwindow","height=650,width=650,top=30,left=350,location=no, status=no,resizable=no"); 
 }
 //分别显示的控件
 function show_div(obj,obj1){
 	var switch_obj=getcookie("switch");
 	
 	$("#my_status").hide();
 	$("#jump").hide();
 	$("#release").hide();
 	$("#search_item").hide();
 	$("#search_me").hide();
 	$("#change_pass").hide();
 	$("#"+obj).show();if(switch_obj==obj)return;
 	if(obj=="jump"&&obj1=="false"){
 	window.location.href="http://localhost:8081/user_status?user_uid=";
 	}
 	if(obj=="search_me"&&obj1=="false"){
 	window.location.href="http://localhost:8081/user_status?user_uid="+uid;
 	}
 	 deletecookie("switch");
	 setcookie("switch",obj,1000*3600*24*30);
 }
 //进行邮箱发送
 function get_v(){
 	 $.ajax({
	   url:"get_verification_api",
	   method:"post",
	   data:{
	   user_uid:uid,
	   qq:qq
	   },
	   dataType:"json",
	   success:function(data){
	   if(data.status=="success")
	   alert("成功发送");
	   },
	   error:function(data)
	   { alert("no");}
	   });
 
 }